package com.itheima03.day14Test;

import java.util.ArrayList;
import java.util.Random;

/**
 * @author 刘全
 * @date 2021/8/6
 */
public class MyRunnable6 implements Runnable {
    private int[] arr = {85, 100, 60, 10, 50, 20, 22, 8};
    private int len = arr.length;
    private ArrayList<Integer> list = new ArrayList<>();
    private Object obj = new Object();

    public MyRunnable6() {
    }
    /*{
        for (int i = 0; i < arr.length; i++) {
            list.add(arr[i]);
        }
    }*/

    @Override
    public void run() {
        Random r = new Random();
        int sum = 0;
        while (true) {
            synchronized (obj) {
                if (len == arr.length) {
                    for (int i = 0; i < arr.length; i++) {
                        list.add(arr[i]);
                    }
                }
                if (list.size() <= 0) {
                    System.out.println(Thread.currentThread().getName() + "总共抽中了" + sum + "元奖金");
                    break;
                } else {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    int i = r.nextInt(list.size());
                    System.out.println(Thread.currentThread().getName() + "抽中了" + list.get(i) + "元奖金");
                    sum += list.get(i);
                    list.remove(i);
                    len--;
                }
            }
        }
    }
}
